﻿var _totalTtimeSpan = 10;
var _interval = 2000;
var _myLover = null;
var _choices = Array('1', '2', '3', '4');
var _allChoices = Array('1', '2', '3', '4', '5', '6', '7', '8', '9');
var _allChoicesNum = 9;
var _allChoicesFlag = Array();
var _score = 0;
var _timer = null;
var _startTime = null;
var _loadedNum = 0;
var _beatiWith = 300;
var _beatiHeight = 300;
function step1_chooseLover()
{	
    $("#score").hide();
	// prepare people for choose
	renderChoice();
	
	// show screen
    $("#step1_chooseLover").show();
    $("#step2_gaming").hide();

    // bind event
    $("#step1_chooseLover img").each(function()
    {
    	$(this).each(
    		function ()
    		{
    			$(this).bind('click', function()
    			{
    				_myLover = $(this).attr('id');
    				step2_gaming();
    			});
    		}
    	);
    });
}
function step2_gaming()
{
	$("#score").show();
	// prepare random people
	renderRandomPeople();
	
	// bind event
    $("#step2_gaming img").each(function()
    {
    	$(this).each(
    		function ()
    		{
    			$(this).unbind('click');
    			$(this).bind('click', function()
    			{
    				gameDeal(this);
    			});
    		}
    	);
    });
	
	_score = 0;
	
	// show screen, and begin the game
    $("#step1_chooseLover").hide();
    
    _timer = window.setInterval("gameDeal()",_interval);
    
    // start the game
    $("#step2_gaming").show();
    _startTime = Math.round(new Date().getTime()/1000) ;
}
function gameDeal(o)
{
	if (Math.round(new Date().getTime()/1000) >= (_startTime + _totalTtimeSpan))
	{
		gameStop();
		showResult();
	}
	if (o != null)
	{
		if ($(o).attr('id') == _myLover)
		{
			_score += 4;
		}
		else
		{
			_score -= 2;
		}
		$('#score').html(_score + ' 分');
		clearInterval(_timer);
		_timer = window.setInterval("gameDeal()", _interval);
	}
	renderRandomPeople();
}
function gameStop()
{
	$("img").each(function()
    {
    	$(this).each(
    		function ()
    		{
    			$(this).unbind('click');
    		}
    	);
    });
}
function showResult()
{
	clearInterval(_timer);
	$('#score').html('总分：' + _score);
}
function init()
{
	_beatiWidth = ($(window).width() - 100)/3;
	_beatiHeight = ($(window).height() - 100)/3;

	fixSize($('#mainCanvas'), _beatiWidth*3, _beatiHeight*3);
	//alert($('#img_'+ _allChoices[0]).width());
	
	$("#imageSources").hide();
	for(var choice in _allChoices)
	{
		$('#imageSources').append('<img id="img_'+_allChoices[choice]+'"src="images/'+_allChoices[choice]+'.png" />');
	}
	
	checkAllLoad();
	
}
function fixSize(o, w, h)
{
	if (o.width()/o.height() >= w/h)
	{
		o.width(w);
	}
	else
	{
		o.height(h);
	}
	
}
function checkAllLoad()
{
	loading();
	for(var choice in _allChoices)
	{
		$('#img_'+_allChoices[choice]).load(
			function ()
			{
				fixSize($(this), _beatiWidth, _beatiHeight);
				_loadedNum++;
				if (_loadedNum == _allChoicesNum)
				{
					
					loaded();
					step1_chooseLover();
				}
			}
		);
	}
}
function loading()
{
	$('#loading').show();
}
function loaded()
{
	$('#loading').hide();
}
function renderChoice()
{
	$('#step1_chooseLover td').each(
		function (i)
		{
			$(this).append($('#img_' + _choices[i]));
		}
	);
}
function renderRandomPeople()
{
	_allChoicesFlag = Array();
	// render
	$('#step2_gaming td').each(
		function (i)
		{
			r = GetRandomNum(0, 8);
			while (_allChoicesFlag[r] == true)
			{
				r = GetRandomNum(0, 8);
			}
			_allChoicesFlag[r] = true;
			$(this).append($('#img_' + _allChoices[r]));
		}
	);
}
function GetRandomNum(Min,Max){  
    var Range = Max - Min;  
    var Rand = Math.random();  
    return(Min + Math.round(Rand * Range));  
}